#include "TabWidget.h"
#include <QtDebug>

TabBar::TabBar(QWidget* parent):QTabBar(parent)
{
	ActClose = new QAction(tr("Close"),this);
	connect(ActClose,SIGNAL(triggered()),this,SIGNAL(Close()));
	ActCloseAll = new QAction(tr("Close All"),this);
	connect(ActCloseAll,SIGNAL(triggered()),this,SIGNAL(CloseAll()));
	ActCloseOther = new QAction(tr("Close Other"),this);
	connect(ActCloseOther,SIGNAL(triggered()),this,SIGNAL(CloseOther()));
	PopMenu  = new QMenu(this);
	PopMenu->addAction(ActClose);
	PopMenu->addAction(ActCloseAll);
	PopMenu->addAction(ActCloseOther);
}

TabBar::~TabBar(void)
{
	delete ActClose;
	delete ActCloseAll;
	delete ActCloseOther;
	delete PopMenu;

}

void TabBar::mousePressEvent(QMouseEvent* e)
{
	if( e->button() == Qt::LeftButton)
	{
		//do something ??	
	}
	else if( e->button() == Qt::RightButton)
	{
		QPoint pos = e->pos();
		int index = tabAt(pos);	
		if ( index <0 ) 
			return ;
		TabWidget* parent = dynamic_cast<TabWidget*>(parentWidget());
		parent->setCurrentIndex(index);
		pos = mapToGlobal(pos);
		PopMenu->popup(pos);
	}
	QTabBar::mousePressEvent(e);
}


TabWidget::TabWidget(QWidget* parent):QTabWidget(parent)
{
	YTabBar = new TabBar(this);
	setTabBar(YTabBar);
	connect(YTabBar,SIGNAL(Close()),this,SLOT(CloseTab()));
	connect(YTabBar,SIGNAL(CloseOther()),this,SLOT(CloseOtherTab()));
	connect(YTabBar,SIGNAL(CloseAll()),this,SLOT(CloseAllTab()));
}

TabWidget::~TabWidget(void)
{
	delete YTabBar;
}

//QsciScintilla* TabWidget::GetScintilla(const QWidget* Widget)
Scintilla* TabWidget::GetScintilla(const QWidget* Widget)
{
	//QsciScintilla* Pointer  = Widget->findChild<QsciScintilla*>();
	Scintilla* Pointer  = Widget->findChild<Scintilla*>();
        Q_ASSERT(Pointer!=NULL);
	if(Pointer->isModified())
	{
		int ret = QMessageBox::warning(this, SoftwareName,
                     tr("The document has been modified.\n"
                        "Do you want to save your changes?"),
                     QMessageBox::Yes | QMessageBox::Default,
                     QMessageBox::Cancel | QMessageBox::Escape);
        	if (ret == QMessageBox::Yes)
		{
			emit TabSaveFile();
		}
        	//else if (ret == QMessageBox::Cancel)
            	return Pointer;
	}
	else
	{
		return  Pointer;
	}
}

void TabWidget::CloseTab(void)
{
	QWidget* Widget = currentWidget();
	int Index = indexOf(Widget);
	if ( Index <0 )
		return ;
	//QsciScintilla* Pointer = GetScintilla(Widget);
	Scintilla* Pointer = GetScintilla(Widget);
	if(Pointer == NULL)
	{
		return ;
	}

	int Count = count();
	int NextIndex = Index +1;
	if ( NextIndex > (Count-1))
	{
		// delete last tab
		int PrevIndex = Index -1;
		if (PrevIndex < 0 )
		{
			emit CreateNewFile();
		}
		else
		{	
			setCurrentIndex(PrevIndex);
		}	
	}
	else if( NextIndex <= Count)
		setCurrentIndex(NextIndex);
	removeTab(Index);
	delete Pointer;
	delete Widget;
    if(Index>=TabVec.size())
        return;
	TabVec.remove(Index);
	
}

void TabWidget::CloseOtherTab(void)
{
	int Index = currentIndex();
	QString CurTabName = tabText(Index);
	QWidget* CurWidget = widget(Index);
	removeTab(Index);
	int Count = count();
	for (register int i=Count -1; i>=0; i--)
	{
		QWidget* Widget = widget(i);
		if (Widget == NULL)
			continue;
		//QsciScintilla* Pointer  = Widget->findChild<QsciScintilla*>();
		Scintilla* Pointer = GetScintilla(Widget);
		if(Pointer == NULL)
		{
			continue ;
		}
		removeTab(i);
		TabVec.remove(i);
		delete Pointer;
		delete Widget;
		TabVec.remove(i);
	}
	addTab(CurWidget,CurTabName);
}

void TabWidget::CloseAllTab(void)
{
	int Count = count();
	for(register int i = 0; i <Count ;i++)
	{
		CloseTab();
	}
}

void TabWidget::addTabInfo(TabInfo* TInfo)
{
	TabVec.append(TInfo);
}

TabInfo* TabWidget::GetCurTabInfo(void)
{
	int Index = currentIndex();
	if(Index >=TabVec.size())
	{
		return NULL;
	}
	else if(Index >=0)
	{
		return TabVec[Index];
	}
}

void TabWidget::setTabInfo(int index,const QString& fn)
{
	m_setTabInfo(index,fn);
}

void TabWidget::m_setTabInfo(int index,const QString& fn)
{
	if( index >= TabVec.size() )
	{
		TabInfo* Ti = new TabInfo(QFileInfo(fn).absolutePath(),QFileInfo(fn).fileName());
		TabVec.append(Ti);
	}
	else
	{
		TabInfo* temp = TabVec[index];
		delete temp;
		TabVec[index] = new TabInfo(QFileInfo(fn).absolutePath() ,QFileInfo(fn).fileName());
	}
}

void TabWidget::setTabInfo(const QString& fn)
{
	int index = currentIndex();
	m_setTabInfo(index,fn);
}
